package org.jmc.refmanager.web.wicket.form;

import java.util.Date;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.jmc.refmanager.domain.model.OrgUnit;
import org.jmc.refmanager.domain.model.OrgUnit.Complement;
import org.jmc.refmanager.jsr303validators.JSR303FormValidator;
import org.jmc.refmanager.web.wicket.markup.html.form.ChoiceRenderer.EnumDropDownChoice;
import org.jmc.refmanager.web.wicket.model.SaChoicesModel;

public class ServiceNewForm extends Form<OrgUnit> {
	private static final long serialVersionUID = 1L;

	public ServiceNewForm(String id, final IModel<OrgUnit> model, final ModalWindow codePostalPicker, final ModalWindow openParentPicker) {
		super(id, model);
		setModel(new CompoundPropertyModel<OrgUnit>(model));
		//setOutputMarkupId(false);
		
		OrgUnit service = model.getObject();
		
		// Generer aleatoirement le code UO sur 8 chars (maj et chiffre)
		add(new Label("codeUO", new PropertyModel<String>(model, "codeUO")));
		add(new RequiredTextField<String>("libCourt", 
				new PropertyModel<String>(model, "libCourt")));
		add(new RequiredTextField<String>("libLong", 
				new PropertyModel<String>(model, "libLong")));
		add(new RequiredTextField<String>("libComplet", 
				new PropertyModel<String>(model, "libComplet")));

		OrgUnit parent = service.getParent();
		if (parent != null) {
			add(new Label("parentType", new PropertyModel<String>(model, "parent.type")));
		} else {
			add(new Label("parentCodeUO", "n/a"));
			add(new Label("parentType", "n/a"));
		}

		add(new TextField<String>("numRue", 
				new PropertyModel<String>(model, "numRue")));

		final TextField<String> email = new TextField<String>("mail", new PropertyModel<String>(model, "mail"));
		email.add(EmailAddressValidator.getInstance());
		email.setRequired(service.isSaa());
		add(email);
		
		final Label mailRequired = new Label("mailRequired", "*");
		mailRequired.setVisible(service.isSaa());
		add(mailRequired);

		final TextField<String> telephone = new TextField<String>("telephone", new PropertyModel<String>(model, "telephone"));
		telephone.setRequired(service.isSaa());
		add(telephone);

		final Label telephoneRequired = new Label("telephoneRequired", "*");
		telephoneRequired.setVisible(service.isSaa());
		add(telephoneRequired);

		final TextField<String> rescom = new TextField<String>("rescom", new PropertyModel<String>(model, "rescom"));
		rescom.setRequired(service.isSaa());
		add(rescom);

		final Label rescomRequired = new Label("rescomRequired", "*");
		rescomRequired.setVisible(service.isSaa());
		add(rescomRequired);

		DropDownChoice<Boolean> saaOrSao = new DropDownChoice<Boolean>("saa", 
				new PropertyModel<Boolean>(model, "saa"), new SaChoicesModel(), new IChoiceRenderer<Boolean>() {
					private static final long serialVersionUID = 1L;

					@Override
					public Object getDisplayValue(Boolean key) {
						return SaChoicesModel.get(key);
					}

					@Override
					public String getIdValue(Boolean key, int arg1) {
						return String.valueOf(key);
					}
		}) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSelectionChanged(Boolean newSelection) {
				super.onSelectionChanged(newSelection);
				// Changement de l'affichage
				telephone.setRequired(newSelection);
				email.setRequired(newSelection);
				rescom.setRequired(newSelection);
				telephoneRequired.setVisible(newSelection);
				mailRequired.setVisible(newSelection);
				rescomRequired.setVisible(newSelection);
			}
			
			@Override
			protected boolean wantOnSelectionChangedNotifications() {
				return true;
			}
		};
		saaOrSao.setRequired(true);
		add(saaOrSao);

		add(new EnumDropDownChoice<Complement>("complementNumRue", 
				new PropertyModel<Complement>(model, "complementNumRue"), Complement.class));

		add(new RequiredTextField<String>("adresse", 
				new PropertyModel<String>(model, "adresse")));

		add(new TextField<String>("complementAdresse", 
				new PropertyModel<String>(model, "complementAdresse")));
		
		add(new RequiredTextField<String>("adressePostale", 
				new PropertyModel<String>(model, "adressePostale")));

		RequiredTextField<Date> dateEffet1 = new RequiredTextField<Date>("dateEffet1", new PropertyModel<Date>(model, "dateEffet1"), Date.class);
//		dateEffet1.add(new DatePicker() {
//			private static final long serialVersionUID = 1L;
//
//			/* (non-Javadoc)
//			 * @see org.apache.wicket.extensions.yui.calendar.DatePicker#getDatePattern()
//			 */
//			@Override
//			protected String getDatePattern() {
//				return "dd/MM/yyyy";
//			}
//			
//		});
		dateEffet1.setOutputMarkupId(false);
		add(dateEffet1);

		AjaxLink<String> openWindowLink = new AjaxLink<String>("openWindowLink") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				target.appendJavascript("Wicket.Window.unloadConfirmation = false;");
				codePostalPicker.show(target);
			}
		};
		add(openWindowLink);

		AjaxLink<String> openParentCodeUOLink = new AjaxLink<String>("openParentCodeUOLink") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				target.appendJavascript("Wicket.Window.unloadConfirmation = false;");
				openParentPicker.show(target);
			}
		};
		add(openParentCodeUOLink);

		TextArea<String> motif = new TextArea<String>("motifCreation");
		motif.setRequired(true);
		add(motif);
		
		// Validateur JSR303
		add(new JSR303FormValidator());
	}
}
